using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;

namespace Athena.Plugins
{
    public class SliderTreeView : TreeView
    {

        public SliderTreeView()
            : base()
        {
            NodeMouseClick += new TreeNodeMouseClickEventHandler(SliderTreeView_NodeMouseClick);
            AfterSelect += new TreeViewEventHandler(SliderTreeView_AfterSelect);
            KeyDown += new KeyEventHandler(SliderTreeView_KeyUp);

            this.ItemHeight = 28;   
            //this.it
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left: 
                case Keys.Right:
                    return false;
                default:
                    break;
            }
            return base.ProcessDialogKey(keyData);
        }



        void SliderTreeView_KeyUp(object sender, KeyEventArgs e)
        {
            TreeNode oNode = SelectedNode;
            if (oNode != null && oNode is SliderTreeViewNode && _oCurrent != null && _oCurrent == oNode)
            {
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        _oCurrent.SliderControl.SmallChangeDown();
                        e.Handled = true;
                        break;
                    case Keys.Right:
                        _oCurrent.SliderControl.SmallChangeUp();
                        e.Handled = true;
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if (_oCurrent != null && (oNode != _oCurrent || !(oNode is SliderTreeViewNode)) )
                    HideSliderControl();
            }
        }

        void SliderTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ShowSliderControl(e.Node);
        }

        void SliderTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //ShowSliderControl(e.Node);
            SelectedNode = e.Node;
        }

        private SliderTreeViewNode _oCurrent = null;
        

        void SliderControl_Leave(object sender, EventArgs e)
        {
            HideSliderControl();
        }
        private void ShowSliderControl(TreeNode oNode)
        {
            if (this._oCurrent != null)
                HideSliderControl();

            if (!(oNode is SliderTreeViewNode))
            {
                return;
            }
             


            this._oCurrent = (SliderTreeViewNode)oNode;

            // Need to add the node's SliderControl to the
            // TreeView's list of controls for it to work
            this.Controls.Add(this._oCurrent.SliderControl);

            // Set the bounds of the SliderControl, with
            // a little adjustment to make it look right
            this._oCurrent.SliderControl.SetBounds(
                this._oCurrent.Bounds.X - 1,
                this._oCurrent.Bounds.Y - 2,
                this.ClientRectangle.Width - this._oCurrent.Bounds.X,
                this._oCurrent.Bounds.Height);

            this._oCurrent.SliderControl.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;

            // Listen to the SelectedValueChanged
            // event of the node's SliderControl
            this._oCurrent.SliderControl.Leave += new EventHandler(SliderControl_Leave);

            // Now show the SliderControl
            this._oCurrent.SliderControl.Show();
        }
        public void HideSliderControl()
        {

            if (this._oCurrent == null)
                return;

            lock (this._oCurrent)
            {
                // Unregister the event listener
                this._oCurrent.SliderControl.Leave -=
                                     SliderControl_Leave;

                // Copy the selected text from the SliderControl to the TreeNode
                this._oCurrent.Text = this._oCurrent.SliderControl.ToString();

                // Hide the SliderControl
                this._oCurrent.SliderControl.Hide();

                // Remove the control from the TreeView's
                // list of currently-displayed controls
                this.Controls.Remove(this._oCurrent.SliderControl);

                // And return to the default state (no SliderControl displayed)
                this._oCurrent = null;
            }
            

        }
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            HideSliderControl();
            base.OnMouseWheel(e);
        }

    }
}
